package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzq.annotation.EnableMyPage;
import com.zzq.domain.dto.SmilePhotographerDTO;
import com.zzq.domain.search.PhotographerSearchParamet;
import com.zzq.enumerate.AuditEnum;
import com.zzq.result.SmileResult;
import com.zzq.domain.entity.SmilePhotographer;
import com.zzq.service.PhotographerAdminService;
import com.zzq.service.SmilePhotographerService;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PhotographerAdminServiceImpl implements PhotographerAdminService {

    @Autowired
    SmilePhotographerService photographerService;

    @Override
    @EnableMyPage
    public SmileResult list(PhotographerSearchParamet searchParamet) {
        List<SmilePhotographerDTO> list = photographerService.getPhotographerDTOList(searchParamet);
        return SmileResult.sucess("查询成功", list);
    }

    @Override
    @Transactional
    public SmileResult update(SmilePhotographer photographer) {
        photographer.setAudit(null);
        photographer.setPhotographerAccount(photographer.getPhotographerPhone());
        LambdaQueryWrapper<SmilePhotographer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(
                ObjectUtils.isEmpty(photographer.getPhotographerAccount()),
                SmilePhotographer::getPhotographerAccount,
                photographer);
        photographerService.updateById(photographer);
        return SmileResult.sucess("更新成功");
    }

    @Override
    @Transactional
    public SmileResult del(String photographerAccount) {
        LambdaQueryWrapper<SmilePhotographer> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(photographerAccount)) {
            queryWrapper.eq(
                    SmilePhotographer::getPhotographerAccount,
                    photographerAccount);
            photographerService.remove(queryWrapper);
            return SmileResult.sucess("删除成功");
        }else {
            return SmileResult.waring("删除失败");
        }

    }

    @Override
    @Transactional
    public SmileResult setAudit(String photographerAccount, Boolean isby) {
        LambdaUpdateWrapper<SmilePhotographer> queryWrapper = new LambdaUpdateWrapper();
        queryWrapper.eq(
                !ObjectUtils.isEmpty(photographerAccount),
                SmilePhotographer::getPhotographerAccount,
                photographerAccount);
        if (isby) {
            queryWrapper.set(SmilePhotographer::getAudit, AuditEnum.AUDITED.getName());
        } else {
            queryWrapper.set(SmilePhotographer::getAudit, AuditEnum.NOAUDITED.getName());
        }
        photographerService.update(queryWrapper);
        return SmileResult.sucess("审核成功");
    }

}